package org.huawei.controller;

import javafx.stage.Stage;
import org.huawei.dao.PermissionDao;
import org.huawei.dao.RolePermissionDao;
import org.huawei.model.domain.Permission;
import org.huawei.annotations.RateLimit;

import java.sql.SQLException;
import java.util.List;

public class PermissionController {
    private Stage stage;
    private PermissionDao permissionDao;
    private RolePermissionDao rolePermissionDao;

    public PermissionController(Stage stage) {
        this.stage = stage;
        this.permissionDao = new PermissionDao();
        this.rolePermissionDao = new RolePermissionDao();
    }

    @RateLimit
    public List<Permission> getUserPermissionsByRoleId(int roleId) throws SQLException {
        List<Integer> permissionIds = rolePermissionDao.getEnabledPermissionIdsByRoleId(roleId);
        return permissionDao.getPermissionsByIds(permissionIds);
    }

    @RateLimit
    public void updateRolePermission(int roleId, int permissionId, boolean isEnabled) {
        rolePermissionDao.updateRolePermission(roleId, permissionId, isEnabled);
    }

    @RateLimit
    public boolean loadRolePermissionStatus(int roleId, int permissionId) {
        return rolePermissionDao.loadRolePermissionStatus(roleId, permissionId);
    }

}
